<template>
	<el-container>
		<el-header>
			<div class="left-panel"></div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model.trim="reqParams.sn" clearable placeholder="输入SN查询" />
					<el-input v-model.trim="reqParams.barcode" clearable placeholder="输入物料号查询" />
					<el-button icon="el-icon-search" type="primary" @click="search" />
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable
				ref="table"
				:tableName="产品耗料"
				:api-obj="apiObj"
				:params="reqParams"
				:column="column"
				row-key="id"
				hideContextMenu>
				<!-- 固定列-选择列 -->
				<el-table-column label="#" type="index" width="40"></el-table-column>
				<el-table-column align="center" fixed="right" label="操作" width="140">
					<template #default="scope">
						<el-popconfirm title="确定解绑吗？" @confirm="unbound(scope.row, scope.$index)">
							<template #reference>
								<el-button text type="primary" size="small" :disabled="scope.row.isUnbound"
									>解绑</el-button
								>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
				<template #isUnbound="{ data }">
					<el-tag disable-transitions :type="data.isUnbound ? 'danger' : 'success'">
						{{ data.isUnbound ? "已解绑" : "绑定" }}
					</el-tag>
				</template>
			</scTable>
		</el-main>

		<modify ref="modify" @complete="complete" />
	</el-container>
</template>

<script>
export default {
	data() {
		return {
			apiObj: this.$API.prodmaterialconsume.page,
			reqParams: {
				sn: "",
				barcode: "",
			},
			column: [
				{ prop: "id", label: "id", width: 100, hide: true },
				{ prop: 'barcode', label: '物料条码', width: 240 },
				{ prop: 'materialCode', label: '物料编号', width: 160 },
				{ prop: 'sn', label: 'SN', width: 240 },
				{ prop: 'lineCode', label: '产线编号', width: 100 },
				{ prop: 'opCode', label: '工位编号', width: 100 },
				{ prop: 'procCode', label: '工序编号', width: 100 },
				{ prop: 'feedingTime', label: '上料时间', width: 140 },
				{ prop: 'isUnbound', label: '绑定状态', width: 100 },
			],
		};
	},
	methods: {
		search() {
			this.$refs.table.refresh();
		},
		async unbound(row) {
			const resp = await this.$API.prodmaterialconsume.unbound.put(row);
			if (resp.code == 200) {
				this.$message.success("解绑成功");
				this.$refs.table.refresh();
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		}
	},
};
</script>
